function onFrontHomepageLoad() {
    setUserInfo();
    setOnSalePanel();
    setHotProductPanel()
}

function setUserInfo() {
    const userName = localStorage.getItem(ITEM_KEY_USER_NAME);
    const builder = new StringBuilder();
    if (isStringNotEmpty(userName)) {
        builder.append("<a href=\"#\" class=\"shopping\">购物车</a>");
        builder.append("<label>");
        builder.append(userName);
        builder.append("</label>");
        const role = localStorage.getItem(ITEM_KEY_USER_ROLE);
        if (role !== ROLE_USER) {
            builder.append("<a href=\"manae/index.html\">进入后台</a>" );
        }
        builder.append("<a href=\"#\" onclick=\"onLogoutClick()\">退出登录</a>");
    }else {
        builder.append("<a href=\"login.html\">登录</a>");
        builder.append("<a href=\"register.html\">注册</a>");
    }
    builder.append("<a href=\"guestbook.html\">留言</a>");
    $("#functionPanel").append(builder.toString());
}

/**
 * 今日特价面板
 */
function setOnSalePanel(){
    const onSaleProductCount=8;
    $.ajax({
        url: URL_HEAD_GOODS + "/randomList/"+onSaleProductCount,
        type : "get",
        success: function(result) {
            const productCount = result.data.length;
            if (productCount > 0) {
                for (let i = 0; i < productCount; i++) {
                    const product = result.data[i];
                    addPanelNewProduct("#onSalePanel",product);
                    console.log(product)
                }
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            showError("导入商品展示失败",xhr,textStatus,errorThrown);
        }
    });
}

/**
 * 热卖面板
 */
function setHotProductPanel(){
    const HotProductCount=8;
    $.ajax({
        url: URL_HEAD_GOODS + "/randomList/"+HotProductCount,
        type : "get",
        success: function(result) {
            const productCount = result.data.length;
            if (productCount > 0) {
                for (let i = 0; i < productCount; i++) {
                    const product = result.data[i];
                    addPanelNewProduct("#hotProductPanel",product);
                    console.log(product)
                }
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            showError("导入商品展示失败",xhr,textStatus,errorThrown);
        }
    });
}

/**
 * 分类下所有商品面板
 * @param catId
 */
function setAllSameCatProductPanel(catId){
    $.ajax({
        url: URL_HEAD_GOODS + "/catList/"+catId,
        type : "get",
        success: function(result) {
            const productCount = result.data.length;
            if (productCount > 0) {
                for (let i = 0; i < productCount; i++) {
                    const product = result.data[i];
                    addPanelNewProduct("#allSameCatProductPanel",product);
                    console.log(product)
                }
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            showError("导入商品展示失败",xhr,textStatus,errorThrown);
        }
    });
}

/**
 * 商品面板中添加一项商品
 * @param panel
 * @param product
 */
function addPanelNewProduct(panelId,product){
    const builder = new StringBuilder();
    builder.append("<li><dl><dt><a href='product-view.html?id=");
    builder.append(product.goodsId);
    builder.append("' target='_blank'><img src='images/products/"+product.goodsId+".png'/>");
    builder.append("</a></dt><dd class='title'><a href='product-view.html' target='_blank'>");
    builder.append(product.name);
    builder.append("</a></dd><dd class='price'>￥");
    builder.append(product.price);
    builder.append("</dd></dl></li>")
    $(panelId).append(builder.toString());
}


function setProductClassInfo() {
    $.ajax({
        url: URL_HEAD_GOODS + TAG_CATEGORY + "/list/nested" ,
        type : "get",
        dataType: "json " ,
        data: {},
        // headers: { "captcha": }
        success:function(result) {
            const categoryCount = result.data.length;
            if (categoryCount > 0) {
                const builder = new StringBuilder();
                for (let i = 0; i < categoryCount ; i++) {
                    const category = result.data[i];
                    builder.append("<dt>");
                    builder.append(category.name);
                    builder.append("</dt>" );
                    const childCategoryCount = category.childCategories.length;
                    for (let j = 0; j < childCategoryCount;j++) {
                        const childCategory = category.childCategories[j];
                        builder.append("<dd><a href=\"product-list.html?parentName=");
                        builder.append(category.name);
                        builder.append("&name=");
                        builder.append(childCategory.name);
                        builder.append("&id=");
                        builder.append(childCategory.catId);
                        builder.append("\">");
                        builder.append(childCategory.name);
                        builder.append("</a></dd>");
                    }
                }
               $("#productClass").append(builder.toString());
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            showError("导入分类管理失败",xhr,textStatus,errorThrown);
        }
    });
}


function setWelcomeLabel() {
    const builder = new StringBuilder();
    builder.append("管理员");
    builder.append(localStorage.getItem(ITEM_KEY_USER_NAME));
    builder.append("您好，今天是");
    builder.append(new Date().toLocaleDateString());
    builder.append("，欢迎回到管理后台。");
    $("#lblWelcome" ).append(builder.toString());
}

function onShoppingClick() {
    alert("on shopping click " );
}
